package org.jeecg.modules.wxuser.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.ApiVersion;
import org.jeecg.config.ApiVersionConstant;
import org.jeecg.config.WebConfig;
import org.jeecg.modules.wxuser.dto.LoginDto;
import org.jeecg.modules.wxuser.dto.WxOpenDataDto;
import org.jeecg.modules.mall.entity.MallUserInfo;
import org.jeecg.modules.wxuser.service.IUserInfoService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Api(tags="商城登录")
@Slf4j
@RestController
@AllArgsConstructor
@EnableSwagger2
@RequestMapping("/mall-api/wxuser")
public class WxUserController extends WebConfig {

    @Resource
    private IUserInfoService userInfoService;
    /**
     * 小程序用户登录
     * @param request
     * @param dto
     * @return
     */
    @ApiOperation(value = "小程序用户登录")
    @PostMapping("/ma-login")
    @ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
    public Result<MallUserInfo> loginMa(HttpServletRequest request, @RequestBody LoginDto dto) {
        try {
            String appId = request.getHeader("app-id");
            if (StringUtils.isBlank(appId)) {
                return Result.error("缺少appid");
            }
            dto.setAppId(appId);
            MallUserInfo userInfo = userInfoService.loginMa(dto);
            return Result.ok(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 通过小程序授权手机号一键登录商城
     * @param dto
     * @return
     */
    @ApiOperation(value = "通过小程序授权手机号一键登录商城")
    @PostMapping("/ma-phone-login")
    @ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
    public Result loginByPhoneMa(HttpServletRequest request, @RequestBody WxOpenDataDto dto) {
        try {
            String token = request.getHeader("third-session");
//            String appId = request.getHeader("app-id");
//            if (StringUtils.isBlank(appId)) {
//                return Result.error("缺少appid");
//            }
//            dto.setAppId(appId);
            MallUserInfo userInfo = userInfoService.loginByPhoneMa(dto, token);
            return Result.ok(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

}
